package com.talent.common.aspect;

import com.talent.common.model.WebLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.annotation.Order;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.HashMap;

/**
 * @ClassName：WebLogAspect
 * @Author: hszhou
 * @Date: 2025/2/15 14:18
 * @Description: 日志切面
 * @Version: 1.0
 */
@Aspect
@Component
@Order(1)
@Slf4j
public class WebLogAspect {


    @Pointcut("execution(* com.talent.service.*.controller.*.*(..))")
    public void webLog() {

    }


    /**
     * 环绕通知
     * @param joinPoint
     * @return
     */
    @Around("webLog()")
    public Object recordWebLog(ProceedingJoinPoint joinPoint) throws Throwable {

        Object result = null;
        WebLog webLog  = new WebLog();
        long startTime = System.currentTimeMillis();

        //执行真正的方法
        result = joinPoint.proceed(joinPoint.getArgs());

        long endTime = System.currentTimeMillis();
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        webLog.setUrl(request.getRequestURL().toString());
        webLog.setUri(request.getRequestURI());

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        webLog.setUsername(authentication==null?"anonymous":authentication.getPrincipal().toString());
        webLog.setIp(request.getRemoteAddr());

        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        String targetClassName = joinPoint.getTarget().getClass().getName();

        webLog.setMethod(targetClassName+"."+method.getName());
        webLog.setDescription("");
        webLog.setParameter(getMethodParameter(method,joinPoint.getArgs()));
        webLog.setResult(result);
        webLog.setCost((int)(endTime - startTime)/1000);
        log.info(webLog.toString());
        return result;
    }

    private Object getMethodParameter(Method method,Object[] args) {

        HashMap<Object, Object> map = new HashMap<>();
        LocalVariableTableParameterNameDiscoverer localVariableTableParameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();

        String[] parameterNames = localVariableTableParameterNameDiscoverer.getParameterNames(method);
        for (int i = 0; i < parameterNames.length; i++) {
            map.put(parameterNames[i],args[i]);
        }

        return map;
    }
}
